package cn.baker.rabbitmq.service;

import cn.baker.rabbitmq.config.TransRabbitProperties;
import cn.baker.rabbitmq.enummeration.TransMessageType;
import cn.baker.rabbitmq.entity.TransMessage;
import cn.baker.rabbitmq.mapper.TransMessageMapper;
import lombok.extern.slf4j.Slf4j;


import java.util.List;
import java.util.UUID;

/**
 * @author yuanyu
 */
@Slf4j
public class TransMessageServiceImpl implements TransMessageService {

    private TransMessageMapper transMessageMapper;
    private TransRabbitProperties transRabbitProperties;

    public TransMessageServiceImpl(TransMessageMapper transMessageMapper, TransRabbitProperties transRabbitProperties) {
        this.transMessageMapper = transMessageMapper;
        this.transRabbitProperties = transRabbitProperties;
    }

    @Override
    public TransMessage messageSendReady(String exchange, String routingKey, String body) {
        String messageId = UUID.randomUUID().toString();
        TransMessage transMessage = TransMessage.builder()
                .id(messageId)
                .service(transRabbitProperties.getService())
                .type(TransMessageType.SEND.toString())
                .exchange(exchange)
                .routingKey(routingKey)
                .queue("")
                .sequence(0)
                .payload(body)
                .build();
        transMessageMapper.insert(transMessage);
        return transMessage;
    }

    @Override
    public void messageSendSuccess(String id) {
        transMessageMapper.delete(id, transRabbitProperties.getService());
    }

    @Override
    public TransMessage messageSendReturn(String id, String exchange, String routingKey, String body) {
        String messageId = UUID.randomUUID().toString();
        TransMessage transMessage = TransMessage
                .builder()
                .id(messageId)
                .service(transRabbitProperties.getService())
                .type(TransMessageType.SEND.toString())
                .exchange(exchange)
                .routingKey(routingKey)
                .sequence(0)
                .queue("")
                .payload(body)
                .build();
        transMessageMapper.insert(transMessage);
        return transMessage;
    }

    @Override
    public void messageResend(String id) {
        TransMessage transMessage = transMessageMapper.selectByIdAndService(id, transRabbitProperties.getService());
        transMessage.setSequence(transMessage.getSequence() + 1);
        transMessageMapper.update(transMessage);
    }

    @Override
    public List<TransMessage> listReadyMessages() {
        return transMessageMapper.selectByTypeAndService(TransMessageType.SEND.toString(), transRabbitProperties.getService());
    }

    @Override
    public void messageDead(String id, String exchange, String routingKey, String queue, String body) {
        TransMessage transMessage = new TransMessage();
        transMessage.setId(id);
        transMessage.setService(transRabbitProperties.getService());
        transMessage.setExchange(exchange);
        transMessage.setRoutingKey(routingKey);
        transMessage.setPayload(body);
        transMessage.setSequence(0);
        transMessage.setType(TransMessageType.DEAD.toString());
        transMessage.setQueue("");
        transMessageMapper.insert(transMessage);
    }

    @Override
    public void messageDead(String id) {
        TransMessage transMessage = transMessageMapper.selectByIdAndService(id, transRabbitProperties.getService());
        transMessage.setType(TransMessageType.DEAD.toString());
        transMessageMapper.update(transMessage);
    }

    @Override
    public TransMessage messageReceiveReady(String id, String exchange, String routingKey, String queue, String body) {
        TransMessage transMessage = transMessageMapper.selectByIdAndService(id, transRabbitProperties.getService());
        if (null == transMessage) {
            transMessage = new TransMessage();
            transMessage.setId(id);
            transMessage.setService(transRabbitProperties.getService());
            transMessage.setExchange(exchange);
            transMessage.setRoutingKey(routingKey);
            transMessage.setQueue(queue);
            transMessage.setPayload(body);
            transMessage.setSequence(0);
            transMessage.setType(TransMessageType.RECEIVE.toString());
            transMessage.setQueue("");
            transMessageMapper.insert(transMessage);
        } else {
            transMessage.setSequence(transMessage.getSequence() + 1);
            transMessageMapper.update(transMessage);
        }
        return transMessage;
    }

    @Override
    public void messageReceiveSuccess(String id) {
        transMessageMapper.delete(id, transRabbitProperties.getService());
    }
}
